<?php
/**
 * @author Martin Pernica
 * @copyright 2008
 */

class DB_MySQLi {
	public $conn;
	public $query_count;
	public $queries_log = array();

	function __construct($host, $user, $pass, $db){
		if( !( $this->conn = mysqli_connect( $host, $user, $pass, $db ) ) ) {
			die( "<h1>Cannot establish connection to DB through <strong>mysqli</strong> driver!</h1>" );
		}
		$this->query("SET NAMES utf8");
		
		return $this->conn;
	}

	function query( $query ) {
        $this->query_count++;
        $query = str_replace('{prefix}', $GLOBALS['db_prefix'], $query);
        $this->logQuery($query);
		$query = mysqli_query( $this->conn, $query );
		return $query;
	}
	
	function multi_query( $query ) {
		$this->query_count++;
		$query = str_replace('{prefix}', $GLOBALS['db_prefix'], $query);
		$this->logQuery($query);
		$query = mysqli_multi_query( $this->conn, $query );
		return $query;
	}

	function fetch_array( $result ) {
		return mysqli_fetch_array( $result );
	}

	function fetch_object( $result ) {
		return mysqli_fetch_object( $result );
	}

	function real_escape_string( $escapestr ) {
		return mysqli_real_escape_string( $this->conn, $escapestr );
	}
	
	function error(){
		return mysqli_error($this->conn);
	}
	
	public function logQuery($query){
		$query = str_replace('{prefix}', $GLOBALS['db_prefix'], $query);
		array_push($this->queries_log, $query);
	}
}

?>